package com.opcode.costa.ui.menu.neworder;

import java.lang.ref.WeakReference;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.opcode.costa.R;
import com.opcode.nebulamobil.base.activities.MultiFragmentActivityBase;
import com.opcode.nebulamobil.base.components.steptracking.PipelineManager;
import com.opcode.nebulamobil.base.components.steptracking.PipelineStep;
import com.opcode.nebulamobil.base.fragments.FragmentBase;
import com.opcode.nebulamobil.utility.ObjectCreator;

/**
 * Bu sınıf, yeni bir sipariş verilmek istendiği zaman çalışacaktır.
 * @author Okan ERDOĞAN
 * 12 Haz 2014
 */
public class NewOrderActivity extends MultiFragmentActivityBase{

	private PipelineManager pipelineManager;
	
	public NewOrderActivity(){
		
	}
	
	
	 protected void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 
		 setContentView(R.layout.new_order_activity_layout); 
		 
		 this.pipelineManager=new PipelineManager("Yeni Sipariş",2);
		 this.pipelineManager.setContainerViewId(R.id.pipeline_container);
		 this.pipelineManager.setStep(new PipelineStep(NewOrderStepOneFragment.class, 0));
		 this.pipelineManager.setStep(new PipelineStep(NewOrderStepTwoFragment.class, 1));
		 super.pipelineForward(pipelineManager, (FragmentBase)ObjectCreator.createClass(NewOrderStepOneFragment.class));
	 } 
	 
	 public void onBackPressed(){	
		 
		 /*
		  * Burada pipelinemanager içerisindeki active step'in NewOrderStepOneFragment olup olmadığına bakılıyor.
		  */
		 if(pipelineManager.getActiveStep()!=null&&pipelineManager.getActiveStep().equals(this.pipelineManager.getStepList().get(0))){
			 System.out.println("NewOrderStepOneFragment");
			 WeakReference<Fragment> fr = pipelineManager.getActiveStep().getwFrag();
			 
			 if(fr != null){
					
			if(((NewOrderStepOneFragment)fr.get()).backPressed())
				return;
			}
			 
		 }else{
			 WeakReference<Fragment> fr = pipelineManager.getActiveStep().getwFrag();
			 
			 if(fr != null){
				 
				 if(((NewOrderStepTwoFragment)fr.get()).backPressed()){
					 pipelineManager.setActiveStep(this.getPipelineManager().getStepList().get(0));
					 WeakReference<Fragment> frNew = pipelineManager.getActiveStep().getwFrag();
				 
					 ((NewOrderStepOneFragment)frNew.get()).backPressed();
					return;
				 }else{
					 return;
				 }
			}
		 }
		 super.onBackPressed();
	 }
}
